﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Dapper;
using Dapper.Contrib.Extensions;
using DapperExtensions;
using MaximServices.Data;
using MaximServices.Model;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;

namespace MaximServices.Repository
{
    public class TestRepository
    {
        private readonly DataBaseRepository _dbrsp;

        public TestRepository(DataBaseRepository dbrsp)
        {
            _dbrsp = dbrsp;
        }

        public void Dispose()
        {
            _dbrsp.Database.Dispose();
        }

        public List<Test> SearchTests(string nome)
        {
            nome = "test";

            if (string.IsNullOrEmpty(nome))
                return _dbrsp.Database.Connection.Query<Test>("SELECT * FROM test ORDER BY Name ASC LIMIT 10").ToList();

            nome = nome.Trim();

            //var predicate = Predicates.Field<Test>(f => f.Name, Operator.Like, nome + "%");
            var predicate = Predicates.Field<Test>(f => f.Name, Operator.Eq, null, true);

            //https://github.com/tmsmith/Dapper-Extensions
            return _dbrsp.Database.GetList<Test>(predicate, null, null, false).ToList();
        }

        public void InsertTest()
        {
            var test = new Test()
            {
                Name = Guid.NewGuid().ToString().Substring(0, 10),
                Age = 10
            };

            _dbrsp.Database.Insert<Test>(test, null);
        }
    }
}
